﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

using XMK.Framework.Model.Ajax;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Framework.Config;
using XMK.Framework.Model.Cond;

namespace XMK.Framework.Web
{
    /// <summary>
    /// Json请求的处理和相应类，在此类中实现动态路由业务逻辑方法并动态调用
    /// </summary>
    public class AjaxDataManager
    {
        /// <summary>
        /// 执行服务端事件
        /// </summary>
        /// <param name="requestInfo">请求附加参数</param>
        /// <returns>执行结果对象</returns>
        internal static ResponseInfo Execute(RequestInfo requestInfo)
        {
            LogHelper.WriteLog(LogType.Debug, "JsonManager反射处理开始：" + requestInfo.Type);

            CommonDataCond dataCond = new CommonDataCond();
            dataCond.ws = new WsCond();
            dataCond.ws.wsId = requestInfo.Type;
            dataCond.ws.userId = requestInfo.UserID;
            dataCond.ws.logId = Guid.NewGuid().ToString("N");
            //dataCond.ws.supplierId = Config.SupplierId;
            dataCond.ws.ipAddr = XMK.Utils.WebHelper.GetLocalIP();

            if (requestInfo.Page != null)
            {
                dataCond.ws.pageSize = requestInfo.Page.PageSize;
                dataCond.ws.curPage = requestInfo.Page.CurrentPage;
            }
            if (requestInfo.ColOrder != null)
            {
                dataCond.ws.sortField = requestInfo.ColOrder.ColName;
                dataCond.ws.sortRule = XMK.Utils.ParseHelper.Parse<int>(requestInfo.ColOrder.Sort);
            }
            // 取得参数
            dataCond.cond = XMK.Utils.JsonHelper.FromJson<Dictionary<string, object>>(requestInfo.Param);

            ResponseInfo responseInfo = new ResponseInfo();
            DateTime startTime = DateTime.Now;
            dataCond.ws.callDatetime = DateHelper.ConvertString(startTime, DateHelper.FORMAT_yyyy_MM_dd_HH_mm_ss_fff);

            CommonDataInfo info = null;

            ICallService callservice = AjaxDataMappingHelper.GetAjaxDataMethod(dataCond.ws.wsId);
            if (callservice != null)
            {
                string condJson = JsonHelper.ToJson(dataCond);
                string strReturn = callservice.CallService(condJson);
                info = JsonHelper.FromJsonMs<CommonDataInfo>(strReturn);
            }
            else
            {
                if (AjaxDataMappingHelper.GetIsCallData())
                    info = SpManager.Execute(dataCond);
                else
                {
                    LogHelper.WriteLog(LogType.Error, "AjaxMapping.xml，AjaxDataMapping.xml，SpMapping.xml找不到方法：" + requestInfo.Type);
                    responseInfo = new ResponseInfo("对应业务处理不存在，业务代码：" + requestInfo.Type);
                    responseInfo.DataDifferTime = DateHelper.GetTimeSpanString(startTime);
                    return responseInfo;
                }
            }

            if (info.ms.code == "0")
            {
                responseInfo.Success(info.ms.msg);
                responseInfo.Data = info.list;                

                if (dataCond.ws.pageSize > 0)
                {
                    responseInfo.Page = new PageInfo();
                    responseInfo.Page.CurrentPage = info.ms.curPage;
                    responseInfo.Page.PageSize = info.ms.pageSize;
                    responseInfo.Page.TotalPage = info.ms.pageCount;
                    responseInfo.Page.Records = info.ms.totalCount;
                }
            }
            else
            {
                responseInfo.Failure(info.ms.msg);
            }
            responseInfo.DataDifferTime = DateHelper.GetTimeSpanString(startTime);
            return responseInfo;
        }
    }
}